package com.cjl.dental_clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjl.dental_clinic.Req.AppointmentListReq;
import com.cjl.dental_clinic.Req.AppointmentReq;
import com.cjl.dental_clinic.entity.Appointment;
import com.cjl.dental_clinic.entity.Patient;
import com.cjl.dental_clinic.mapper.AppointmentMapper;
import com.cjl.dental_clinic.service.AppointmentService;
import com.cjl.dental_clinic.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description:
 * @author: cjl
 * @time: 2024/04/09 14:25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Autowired
    private PatientService patientService;
    @Override
    public Boolean addAppointment(AppointmentReq req) {
        // 预约对象
        Appointment appointment = new Appointment();
        // 患者信息对象
        Patient patient = new Patient();
        // 添加患者信息
        patient.setName(req.getName())
                .setAge(req.getAge())
                .setSex(req.getSex())
                .setPhone(req.getPhone())
                .setAddress(req.getAddress())
                .setSymptom(req.getSymptom())
                .setAllergy(req.getAllergy())
                .setTaboo(req.getTaboo())
                .setDob(req.getDob())
                .setDescription(req.getDescription())
                .setDoctorId(req.getDoctorId())
                .setVisitstatus(0)
                        .initCreateUser();
        boolean save = patientService.save(patient);
        if (save){
            QueryWrapper<Patient> wrapper = new QueryWrapper<>();
            Patient one = patientService.getOne(wrapper.eq("name", req.getName()));
            appointment.setAppointmentDate(req.getAppointmentDate())
                    .setDoctorId(req.getDoctorId())
                    .setPatientId(one.getId())
                    .initCreateUser();
            return save(appointment);
        }
        return false;
    }

    @Override
    public List<AppointmentListReq> listAppointment(AppointmentListReq req) {
        return baseMapper.listAppointment(req);
    }
}
